<?php

namespace App\EventSubscriber\Wuliu;

use App\Base\BaseController;
use App\Entity\Wuliu\WuliuUser;
use App\Event\UserEvent;
use App\Repository\Wuliu\WuliuUserLevelRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class UserSubscriber extends BaseController implements EventSubscriberInterface
{

    public $levelRepository;
    public function __construct(
        WuliuUserLevelRepository $levelRepository
    )
    {
        parent::__construct();
        $this->levelRepository = $levelRepository;
    }


    public static function getSubscribedEvents()
    {
        return [
            UserEvent::USER_REG => 'onUserReg',
            UserEvent::USER_DELETE => 'onUserDelete'
        ];
    }
    public function onUserReg(UserEvent $event)
    {
        $user = $event->getUser();
        //获取默认等级
        $level0 = $this->levelRepository->findOneBy(array(),array('level'=>"ASC"));
        $u = new WuliuUser();
        $u->setUser($user)
            ->setExpireAt(new \DateTime("+ {$level0->getDays()} days"))
            ->setLevel($level0);
        $this->myEm()->persist($u);
        $this->myEm()->flush();
    }
    public function onUserDelete(UserEvent $event)
    {
        $user = $event->getUser();
    }

}
